package spider.daos;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

import spider.interfaces.ResultManipulationInterface;

public class ResultManipulationEntityManager  implements ResultManipulationInterface {
private HibernateTemplate template;
Logger logger=Logger.getLogger(ResultManipulationEntityManager.class);
	public ResultManipulationEntityManager() {
	super();
	logger. log(Priority.INFO, "start the ResultManipulationEntityManager");
	// TODO Auto-generated constructor stub
}

	public HibernateTemplate getTemplate() {
	return template;
}

public void setTemplate(HibernateTemplate template) {
	this.template = template;
}
public  void setSessionFactory(SessionFactory factory){
	this.template=new HibernateTemplate(factory);
}
	public void persist(Object obj) {
		// TODO Auto-generated method stub
template.save(obj);
	}

	public void persist(Object... obj) {
		// TODO Auto-generated method stub
for (Object object : obj) {
	persist(object);
}
	}

	public void update(Object... obj) {
		// TODO Auto-generated method stub
		for (Object object : obj) {
			update(object);
		}
	}

	public void update(Object obj) {
		// TODO Auto-generated method stub
		template.update(obj);
	}

	public void delete(Object... obj) {
		// TODO Auto-generated method stub
		for (Object object : obj) {
			template.delete(object);
		}

	}

	public void delete(Object obj) {
		// TODO Auto-generated method stub
		template.delete(obj);
	}

 
	public List<Object> query(String HQL, Object... params) {
		// TODO Auto-generated method stub
		return template.find(HQL,params);
	}

	public Object queryone(String HQL, Object... params) {
		// TODO Auto-generated method stub
		ArrayList l=(ArrayList) query(HQL, params);
		if(null!=l&&l.size()>0)
			return l.get(0);
		return null;
	}

	public Object findById(Class entityclass, Long id) {
		// TODO Auto-generated method stub
		return template.load(entityclass, id);
	}

	public Object findById(String entityclass, Long id) {
		// TODO Auto-generated method stub
		return template.load(entityclass, id);
	}

	@Override
	public String toString() {
		return "ResultManipulationEntityManager [template=" + template
				+ ", logger=" + logger + "]";
	}

}
